package com.blue.base.common.utils.bean;

import com.blue.base.common.utils.date.LocalDateUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 比对工具类
 *
 * @author liulei
 * @version 1.0
 */
public class CompareUtil {
    private static Logger logger = LoggerFactory.getLogger(LocalDateUtils.class);
    /**
     * 需要追加的记录
     */
    public static final String APPEND = "append";
    /**
     * 需要保留的
     */
    public static final String REMAIN = "remain";
    /**
     * 需要删除的
     */
    public static final String REMOVE = "remove";

    /**
     * 比对两组对象集合，得出结果
     *
     * @param source 源数据
     * @param target 目标数据
     * @return 比对结果
     */
    public static <T> Map<String, List<T>> compareObjectList(List<T> source, List<T> target) {
        Map<String, List<T>> map = Maps.newHashMap();
        List<T> targetCopy = Lists.newArrayList(target);
        targetCopy.retainAll(source);
        map.put(REMAIN, targetCopy);
        source.removeAll(targetCopy);
        map.put(APPEND, source);
        target.removeAll(targetCopy);
        map.put(REMOVE, target);
        logger.info("compare result: {}", map);
        return map;
    }
}
